$(document).ready(function() {
    $("#flex1").flexigrid({
            url: 'open_subject.do',
            dataType: 'json',
            colModel : [
                    {display: 'Subject_Code', name : 'subject_code', width : 100, sortable : true, align: 'center'},
                    {display: 'Subject_Name', name : 'name', width : 200, sortable : true, align: 'center'},
                    {display: 'Lecture', name : 'lecture', width : 400, sortable : true, align: 'left'}
                    ],
            buttons : [
                    {name: 'Add Subject', bclass: 'add', onpress : test},
                    {name: 'Delete Subject', bclass: 'delete', onpress : test},
                    {name: 'Refresh', bclass: 'refresh', onpress : test},
                    {separator: true}
                    ],
            searchitems : [
                    {display: 'Subject_Code', name : 'subject_code'},
                    {display: 'Subject_Name', name : 'name', isdefault: true}
                    ],
            sortname: "subject_code",
            sortorder: "asc",
            usepager: true,
            title: 'Subject List',
            useRp: true,
            rp: 10,
            showTableToggleBtn: true,
            width: 750,
            height: 400
    });   
    function test(com,grid)
    {
        if (com=='Delete')
            {
               if($('.trSelected',grid).length>0){
                       if(confirm('Delete ' + $('.trSelected',grid).length + ' items?')){
                var items = $('.trSelected',grid);
                var itemlist ='';
                    for(i=0;i<items.length;i++){
                                    itemlist+= items[i].id.substr(3)+",";
                            }
                            $.ajax({
                               type: "POST",
                               dataType: "json",
                               url: "subject_delete.jsp",
                               data: "items="+itemlist,
                               success: function(data){
                               alert("Total " +data.total+" record deleted.");
                               $("#flex1").flexReload();
                               }
                             });
                            }
                            } else {
                                    return false;
                            } 
            }
        else if (com=='Add')
            {			
                            addrecwin = window.open('subject_create.htm','subject_create','left=20,top=20,width=500,height=500,toolbar=1,resizable=0');			
            }
        else if (com=='Refresh')
            {			
                            $("#flex1").flexReload();	
            }  		
    }; 
} );